package com.jiyinit.web.core.business.dao;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.dao.mybatis.base.MybatisDaoContext;
import com.jiyinit.core.framework.dao.mybatis.paginator.domain.PageBounds;
import com.jiyinit.web.core.business.dto.FunctionPageRequest;
import com.jiyinit.web.core.business.entity.TabCoreFunEntity;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.business.vo.FunctionVo;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.jdbc.Sql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p></p>
 * <p/>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author zhaolaiwei
 * @version 1.0
 * @date 2017/4/11 17:51
 * @since 1.0
 */
@Repository("core.tabCoreFunDao")
public class TabCoreFunDao {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(TabCoreFunDao.class);

    private String SQLID = "core.tabCoreFunDao.";

    @Autowired
    @Qualifier("core.MybatisDaoContext")
    private MybatisDaoContext mybatisDaoContext;

    public int update(TabCoreFunEntity entity){
        return mybatisDaoContext.update(SQLID+"updateByPrimaryKeySelective",entity);
    }

    public PagingResult<FunctionVo> findPageByParentId(FunctionPageRequest request){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(request.getLimit());
        pageBounds.setPage(request.getPage());
        return mybatisDaoContext.findForPage(SQLID + "findPageByParentId", FunctionVo.class, request.toMap(), pageBounds);
    }

    public PagingResult<FunctionVo> findByPage(FunctionPageRequest request){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(request.getLimit());
        pageBounds.setPage(request.getPage());
        HashMap params = new HashMap();
        params.put("funName",request.getFunName());
        return mybatisDaoContext.findForPage(SQLID + "findByPage", FunctionVo.class, params, pageBounds);
    }

    public PagingResult<FunctionVo> findOrgByPage(Integer orgId,FunctionPageRequest request){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(request.getLimit());
        pageBounds.setPage(request.getPage());
        Map map = request.toMap();
        map.put("orgId",orgId);
        return mybatisDaoContext.findForPage(SQLID+"findOrgByPage",FunctionVo.class,map,pageBounds);
    }

    public List<FunctionVo> findByParentId(Integer parentId){
        return mybatisDaoContext.findAll(SQLID + "findByParentId",FunctionVo.class,parentId);
    }

    public List<TabCoreFunEntity> getAllFun(){
        return mybatisDaoContext.findAll(SQLID + "findAll");
    }

    public TabCoreFunEntity getFunctionById(Integer id){
        return mybatisDaoContext.findOne(SQLID+"selectByPrimaryKey",TabCoreFunEntity.class,id);
    }

    public int save(TabCoreFunEntity function){
        return mybatisDaoContext.save(SQLID+"insertSelective",function);
    }

    public int delete(List<TabCoreFunEntity> list){
        return mybatisDaoContext.batchUpdate(SQLID+"updateByPrimaryKeySelective",list);
    }

    public List<TabCoreFunEntity> selectByParentIds(Integer[] funIds){
        return mybatisDaoContext.findAll(SQLID+"selectByParentIds",TabCoreFunEntity.class,funIds);
    }

    public List<FunctionVo> findFunction(Integer orgId){
        return mybatisDaoContext.findAll(SQLID+"findFunction",FunctionVo.class,orgId);
    }

    public List<FunctionVo> findOrgFunction(Integer orgId,Integer funRoleId){
        HashMap<String,Object> params = new HashMap();
        params.put("orgId",orgId);
        params.put("funRoleId",funRoleId);
        return mybatisDaoContext.findAll(SQLID+"findOrgFunction",FunctionVo.class,params);
    }

    public List<TabCoreFunEntity> findDefaultFunction(){
        return mybatisDaoContext.findAll(SQLID+"findDefaultFunction");
    }

    public List<TabCoreFunEntity> findFunById(Integer orgId,Integer userId){
        HashMap<String,Object> params = new HashMap();
        params.put("orgId",orgId);
        params.put("userId",userId);
        return mybatisDaoContext.findAll(SQLID+"findFunById",TabCoreFunEntity.class,params);
    }
}
